package makovo.data;

import java.io.IOException;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import makovo.utils.Comm;
import makovo.utils.DataAccess;

public class Items {
	public static class DocumentType implements Serializable {
		private static final long serialVersionUID = 1L;
		public int id;
		public String document;
		public String identifier;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getIdentifier() {
			return identifier;
		}

		public void setIdentifier(String identifier) {
			this.identifier = identifier;
		}

		public String getDocument() {
			return document;
		}

		public void setDocument(String document) {
			this.document = document;
		}

		private static Map<Integer, DocumentType> mTypes;

		public DocumentType(int id, String document, String identifier) {
			this.id = id;
			this.document = document;
			this.identifier = identifier;
		}

		public static Map<Integer, DocumentType> getTypes() throws Exception {
			if (mTypes == null)
				fillTypes();

			return mTypes;
		}

		public static DocumentType valueOf(int id) throws Exception {
			return getTypes().get(id);
		}

		public static DocumentType valueByName(String doc) throws Exception {
			Iterator<DocumentType> it = getTypes().values().iterator();
			DocumentType d;

			while (it.hasNext()) {
				d = it.next();

				if (d.identifier.equals(doc))
					return d;
			}
			return null;

		}

		public static int size() {
			return mTypes.values().size();
		}

		private static void fillTypes() throws Exception {
			ResultSet rs;

			rs = DataAccess.getInstance().execResult("sp_obtener_tipos_documentos", null);

			mTypes = new HashMap<>();

			while (rs.next()) {
				mTypes.put(
						rs.getInt(rs.findColumn("id_tipo_documento")),
						new DocumentType(rs.getInt(rs.findColumn("id_tipo_documento")), rs.getString(rs
								.findColumn("tipo_documento")), rs.getString(rs.findColumn("identificador"))));

			}

		}
	}

	public static class GeoInfo {
		private final static String uri = "http://inegifacil.com";
		private final static String cityMethod = "/cities/%s";
		private final static String locMethod = "/towns/%s/%s";
		private static Comm mInstance;

		private static Comm getInstance() {
			if (mInstance == null)
				createCommInstance();

			return mInstance;

		}

		private static void createCommInstance() {
			mInstance = new Comm(uri);
		}

		public static synchronized String getCities(int stateId) throws ClientProtocolException, IOException {
			String method = String.format(cityMethod, String.valueOf(stateId));
			return getInstance().getStringResponse(method);
		}

		public static synchronized String getTowns(int stateId, int cityId) throws ClientProtocolException, IOException {
			String method = String.format(locMethod, String.valueOf(stateId), String.valueOf(cityId));
			return getInstance().getStringResponse(method);
		}
	}
}
